package com.le.tester.book.concurrentchallenge.performanceandscalability11;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * createTime：2025-04-29 10:10
 * description：将一个锁不必要的持有过长的时间，错误的示范
 */
public class AttributeStore {
    private final Map<String, String> attributes = new HashMap<>();


    //synchronized直接加在了整个方法上，实际上这个本身就是不可取的，找出会变化状态的变量并且保证该变量在锁竞争的时候的正确状态即可
    public synchronized boolean userLocationMatches(String name, String regexp) {
        String key = "users." + name + ".location";
        String location = attributes.get(key);
        if (location == null) {
            return false;
        } else {
            return Pattern.matches(regexp, location);
        }
    }
}
